<script setup>
import { useSortStore } from '@/stores/index'
import { ElMessage } from 'element-plus'
import { WarnTriangleFilled } from '@element-plus/icons-vue'

import { ref } from 'vue'

const sortStore = useSortStore()

const dialogFormVisible = ref(false)
const dept = ref({})
const tableData = ref([])
const flag = ref(false)

const selectById = (row) => {
  dept.value = row
  flag.value = false
  dialogFormVisible.value = true
}

// 获取数据
const getSortAll = async () => {
  await sortStore.getSortList()
  tableData.value = sortStore.sort
}

getSortAll()

// 新增分类
const addSort = () => {
  dept.value = {}
  flag.value = true
  dialogFormVisible.value = true
}

const add = async () => {
  if (flag.value) {
    await sortStore.addSort(dept.value)
    await getSortAll()
    ElMessage.success('新增成功')
    dialogFormVisible.value = false
  } else {
    await sortStore.updateSort(dept.value)
    await getSortAll()
    ElMessage.success('修改成功')
    dialogFormVisible.value = false
  }
}

// 删除分类
const deleteById = async (row) => {
  await ElMessageBox.confirm(`你确定要删除（${row.category}）这一分类吗?`, '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    icon: WarnTriangleFilled,
    draggable: true
  })
  await sortStore.deleteSort(row.id)
  await getSortAll()
  ElMessage.success('删除成功')
}
</script>
<template>
  <div>
    <!-- 按钮 -->

    <el-row>
      <el-button type="primary" @click="addSort">+ 新增分类</el-button>
    </el-row>
    <br />
    <!-- 数据表格 -->

    <el-table
      highlight-current-row
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      border
    >
      <el-table-column type="index" width="100" label="序号" header-align="center" align="center">
      </el-table-column>
      <el-table-column
        prop="category"
        label="分类名称"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column label="最后操作时间" header-align="center" align="center">
        <template v-slot="scope">
          {{ scope.row.updateTime ? scope.row.updateTime.replace('T', ' ') : '' }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="420" header-align="center" align="center">
        <template #default="{ row }">
          <el-button size="mini" type="primary" plain @click="selectById(row)">编辑</el-button>

          <el-button size="mini" type="danger" plain @click="deleteById(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新建对话框 -->

    <el-dialog title="保存分类" v-model="dialogFormVisible">
      <el-form :model="dept">
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input
            v-model="dept.category"
            placeholder="请输入分类名称"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>

      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
